#include "MyList.h"

template<class T>
MyList<T> MyList<T>::initList() {
    arr = new T[20];
    size = 0;
    maxSize = 20;
    return *this;
}

template<class T>
bool MyList<T>::destroyList() {
    if(arr == NULL || this == NULL)
        return false;
    delete arr;
    arr = NULL;
    delete this;
    size = 0;
    maxSize = 0;
    return true;
}

template<class T>
int MyList<T>::locateElem(T value) {
    if(arr == NULL) return 0;
    for (int i = 0; i < size; i++) {
        if(arr[i] == value) return i + 1;
    }
    return 0;
}

template<class T>
bool MyList<T>::listInsert(int pos, T value) {
    if(arr == NULL) return false;
    if(pos < 1 || pos > size + 1) return false;

    //看容量是否大
    if(size >= maxSize) {
        T* newArr = new T[maxSize * 2];

        for(int i = 0;i < size;i++) {
            newArr[i] = arr[i];
        }

        delete arr;
        arr = newArr;

        maxSize = maxSize * 2;
    }

    //插入
    for(int i = size;size >= pos;i--) {
        arr[i] = arr[i - 1];
    }

    arr[pos - 1] = value;
    size++;
    return true;
}

template<class T>
T MyList<T>::getElem(int pos) {
    if(arr == NULL) return false;
    if(pos < 1 || pos > size ) return false;

    return arr[pos - 1];
}

template<class T>
bool MyList<T>::listDelete(int pos) {
    if(arr == NULL) return false;
    if(pos < 1 || pos > size ) return false;

    for(int i = pos -1;i < size - 1;i++) {
        arr[i] = arr[i + 1];
    }
    size--;
    return true;
}

template<class T>
void MyList<T>::PrintList() {
    for(int i = 0;i < size;i++) {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

template<class T>
bool MyList<T>::isEmpty() {
    return arr == NULL;
}

template<class T>
int MyList<T>::getLength() {
    return size;
}
